<?php

namespace app\shop\common;

use think\Cache;
use think\Db;
use think\Session;
use think\Controller;
use app\shop\model\ShopGoods as ShopGoodsModel;
use app\shop\model\MemberBrowse as MemberBrowseModel;

class GuessGoods extends Controller
{
    /**
     * 猜你喜欢
     * @return array|false|\PDOStatement|string|\think\Collection
     */
    public function guessLike()
    {
        // 实例化
        $shopGoods = new ShopGoodsModel;
        $memberBrowse = new MemberBrowseModel;

        $guess_like = [];

        //猜你喜欢
        $map['status'] = 1;
        $map['activity'] = 2;
        $map['state'] = 1;

        // 开启全局管理
        if (Cache::get('web_related') == 'Y') {

            $map['related'] = ['eq', 1];
            // $guess_like = $shopGoods->where($map)->field('id,title,price,thumb_img,sale_number,rebate_percentage,evaluate_number,collect')->limit('15')->order(['shop_sort' => 'desc', 'sort' => 'desc', 'create_time' => 'desc'])->select();
            $guess_like = $shopGoods->where($map)->field('id,title,price,thumb_img,sale_number,rebate_percentage,evaluate_number,collect')->limit('15')->order(['rand()' => 'desc'])->select();
        } else {
            // 未开启推送且未登录
            if (Session::has('member_id')) {
                // 未开启推送且登录

                $brose = $memberBrowse->where('member_id', Session::get('member_id'))->field('goods_type')->order('create_time', 'desc')->limit(5)->select();

                // 如果浏览记录为空
                if (empty($brose)) {

                    $guess_like = $shopGoods->where($map)->field('id,title,price,thumb_img,sale_number,rebate_percentage,evaluate_number,collect')->limit('15')->order(['rand()' => 'desc'])->select();
                } else {
                    $new = [];
                    foreach ($brose as $value) {
                        $new[] = $value['goods_type'];
                    }

                    $brose_str = implode(',', $new);

                    $map['goods_id'] = ['in', $brose_str];

                    $guess_like = $shopGoods->where($map)->field('id,title,price,thumb_img,sale_number,rebate_percentage,evaluate_number,collect')->limit('15')->order(['rand()' => 'desc'])->select();

                }


            } else {
                $guess_like = $shopGoods->where($map)->field('id,title,price,thumb_img,sale_number,rebate_percentage,evaluate_number,collect')->limit('15')->order(['rand()' => 'desc'])->select();
            }
        }

        return $guess_like;
    }


    /**
     * 浏览记录
     * @return array
     */
    public function browses()
    {
        $memberId = Session::get('member_id');

        $data = (new MemberBrowseModel())->where(['sun_member_browse.member_id' => $memberId])->with('ShopGoods')->order(['id' => 'desc'])->limit(6)->select()->toArray();

        return $data;
    }


    /**
     * 添加浏览记录
     * @param $goods_id
     * @return bool
     */
    public function recordBrowse($goods_id)
    {
        $memberId = Session::get('member_id');

        // 如果未登录
        if (empty($memberId)) return false;

        //查询数据库是否添加过此商品记录
        $find = (new MemberBrowseModel())->where(['member_id' => $memberId, 'goods_id' => $goods_id])->find();

        // 如果没有记录 则记录
        if (!$find) {
            $type_str = Db::name('ShopGoods')->where('id', $goods_id)->value('goods_str');
            $type_arr = explode(',', $type_str);
            $type = $type_arr[1];

            $ini = [
                'member_id'   => $memberId,
                'goods_id'    => $goods_id,
                'goods_type'  => $type,
                'create_time' => date('Y-m-d', time()),
            ];


            //添加记录
            (new MemberBrowseModel())->save($ini);
        }else{
            $con['create_time'] = date('Y-m-d', time());
            (new MemberBrowseModel())->save($con,['id'=>$find['id']]);
        }

    }

}